package com.macro.mall.pms.service.impl;

import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.common.api.util.StringUtils;
import com.macro.mall.pms.domain.Product;
import com.macro.mall.pms.entity.ProductEntity;
import com.macro.mall.pms.mapper.ProductMapper;
import com.macro.mall.pms.service.ProductService;
import com.macro.mall.pms.service.bo.InsertProductInput;
import com.macro.mall.pms.service.bo.PagedQueryProductInput;
import com.macro.mall.pms.service.bo.UpdateProductInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品信息Service
 *
 * @author xuyanjun
 * @date 2019-12-13
 */
@Slf4j(topic = "ProductServiceImpl")
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 新增商品信息
     *
     * @param input input
     * @return 新增id
     */
    @Override
    public long insert(InsertProductInput input) {
        ProductEntity productEntity = new ProductEntity();
        productEntity.setBrandId(input.getBrandId())
                .setProductCategoryId(input.getProductCategoryId())
                .setFeightTemplateId(input.getFeightTemplateId())
                .setProductAttributeCategoryId(input.getProductAttributeCategoryId())
                .setName(input.getName())
                .setPic(input.getPic())
                .setProductSn(input.getProductSn())
                .setDeleteStatus(input.getDeleteStatus())
                .setPublishStatus(input.getPublishStatus())
                .setNewStatus(input.getNewStatus())
                .setRecommandStatus(input.getRecommandStatus())
                .setVerifyStatus(input.getVerifyStatus())
                .setSort(input.getSort())
                .setSale(input.getSale())
                .setPrice(input.getPrice())
                .setPromotionPrice(input.getPromotionPrice())
                .setGiftGrowth(input.getGiftGrowth())
                .setGiftPoint(input.getGiftPoint())
                .setUsePointLimit(input.getUsePointLimit())
                .setSubTitle(input.getSubTitle())
                .setDescription(input.getDescription())
                .setOriginalPrice(input.getOriginalPrice())
                .setStock(input.getStock())
                .setLowStock(input.getLowStock())
                .setUnit(input.getUnit())
                .setWeight(input.getWeight())
                .setPreviewStatus(input.getPreviewStatus())
                .setServiceIds(input.getServiceIds())
                .setKeywords(input.getKeywords())
                .setNote(input.getNote())
                .setAlbumPics(input.getAlbumPics())
                .setDetailTitle(input.getDetailTitle())
                .setDetailDesc(input.getDetailDesc())
                .setDetailHtml(input.getDetailHtml())
                .setDetailMobileHtml(input.getDetailMobileHtml())
                .setPromotionStartTime(input.getPromotionStartTime())
                .setPromotionEndTime(input.getPromotionEndTime())
                .setPromotionPerLimit(input.getPromotionPerLimit())
                .setPromotionType(input.getPromotionType())
                .setBrandName(input.getBrandName())
                .setProductCategoryName(input.getProductCategoryName())
        ;


        productMapper.insert(productEntity);
        return productEntity.getId();
    }

    /**
     * 递增销量
     *
     * @param productId     商品id
     * @param increaseCount 递增数量
     */
    @Override
    public void increaseSale(long productId, int increaseCount) {
        productMapper.increaseSale(productId, increaseCount);
    }

    /**
     * 修改商品信息
     *
     * @param input input
     */
    @Override
    public void update(UpdateProductInput input) {
        ProductEntity productEntity = productMapper.selectById(input.getId());
        if (productEntity == null) {
            throw new BusinessException(String.format("未能根据Id:%s找到指定的商品信息。", input.getId()));
        }

        productEntity = new ProductEntity();
        productEntity.setId(input.getId())
                .setBrandId(input.getBrandId())
                .setProductCategoryId(input.getProductCategoryId())
                .setFeightTemplateId(input.getFeightTemplateId())
                .setProductAttributeCategoryId(input.getProductAttributeCategoryId())
                .setName(input.getName())
                .setPic(input.getPic())
                .setProductSn(input.getProductSn())
                .setDeleteStatus(input.getDeleteStatus())
                .setPublishStatus(input.getPublishStatus())
                .setNewStatus(input.getNewStatus())
                .setRecommandStatus(input.getRecommandStatus())
                .setVerifyStatus(input.getVerifyStatus())
                .setSort(input.getSort())
                .setSale(input.getSale())
                .setPrice(input.getPrice())
                .setPromotionPrice(input.getPromotionPrice())
                .setGiftGrowth(input.getGiftGrowth())
                .setGiftPoint(input.getGiftPoint())
                .setUsePointLimit(input.getUsePointLimit())
                .setSubTitle(input.getSubTitle())
                .setDescription(input.getDescription())
                .setOriginalPrice(input.getOriginalPrice())
                .setStock(input.getStock())
                .setLowStock(input.getLowStock())
                .setUnit(input.getUnit())
                .setWeight(input.getWeight())
                .setPreviewStatus(input.getPreviewStatus())
                .setServiceIds(input.getServiceIds())
                .setKeywords(input.getKeywords())
                .setNote(input.getNote())
                .setAlbumPics(input.getAlbumPics())
                .setDetailTitle(input.getDetailTitle())
                .setDetailDesc(input.getDetailDesc())
                .setDetailHtml(input.getDetailHtml())
                .setDetailMobileHtml(input.getDetailMobileHtml())
                .setPromotionStartTime(input.getPromotionStartTime())
                .setPromotionEndTime(input.getPromotionEndTime())
                .setPromotionPerLimit(input.getPromotionPerLimit())
                .setPromotionType(input.getPromotionType())
                .setBrandName(input.getBrandName())
                .setProductCategoryName(input.getProductCategoryName())
                .setCommission(input.getCommission());
        ;


        productMapper.updateById(productEntity);
    }

    /**
     * 删除商品信息
     *
     * @param id id
     */
    @Override
    public void delete(long id) {
        productMapper.deleteById(id);
    }

    /**
     * 根据Id获取商品信息
     *
     * @param id id
     * @return 商品信息
     */
    @Override
    public ProductEntity getById(long id) {
        return productMapper.selectById(id);
    }

    /**
     * 根据批量Id获取商品信息
     *
     * @param batchIds 批量ids
     * @return 商品信息集合
     */
    @Override
    public List<ProductEntity> listByBatchIds(List<Long> batchIds) {
        if (batchIds == null || batchIds.isEmpty()) {
            return new ArrayList<>();
        }
        return productMapper.selectBatchIds(batchIds);
    }

    /**
     * 分页查询
     *
     * @param input input
     * @return 分页的商品信息
     */
    @Override
    public Page<ProductEntity> pagedQuery(PagedQueryProductInput input) {
        LambdaQueryWrapper<ProductEntity> queryWrapper = Wrappers.lambdaQuery();

        if (PagedQueryProductInput.SORT_FIELD_NAME.equals(input.getSortField())) {
            queryWrapper.orderBy(true, input.isAsc(), ProductEntity::getSort);
        }
        if (input.getPublishStatusEnum() != null) {
            queryWrapper.eq(ProductEntity::getPublishStatus, input.getPublishStatusEnum().getValue());
        }

        PageHelper.startPage(input.getPageIndex(), input.getPageSize());

        return (Page<ProductEntity>) productMapper.selectList(queryWrapper);
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param productEntity 实体
     * @return product
     */
    @Override
    public Product entityToDomain(ProductEntity productEntity) {
        if (productEntity == null) {
            return null;
        }

        Product product = new Product();
        product.setId(productEntity.getId())
                .setBrandId(productEntity.getBrandId())
                .setProductCategoryId(productEntity.getProductCategoryId())
                .setFeightTemplateId(productEntity.getFeightTemplateId())
                .setProductAttributeCategoryId(productEntity.getProductAttributeCategoryId())
                .setName(productEntity.getName())
                .setPic(productEntity.getPic())
                .setProductSn(productEntity.getProductSn())
                .setDeleteStatus(productEntity.getDeleteStatus())
                .setPublishStatus(productEntity.getPublishStatus())
                .setNewStatus(productEntity.getNewStatus())
                .setRecommandStatus(productEntity.getRecommandStatus())
                .setVerifyStatus(productEntity.getVerifyStatus())
                .setSort(productEntity.getSort())
                .setSale(productEntity.getSale())
                .setPrice(productEntity.getPrice())
                .setPromotionPrice(productEntity.getPromotionPrice())
                .setGiftGrowth(productEntity.getGiftGrowth())
                .setGiftPoint(productEntity.getGiftPoint())
                .setUsePointLimit(productEntity.getUsePointLimit())
                .setSubTitle(productEntity.getSubTitle())
                .setDescription(productEntity.getDescription())
                .setOriginalPrice(productEntity.getOriginalPrice())
                .setStock(productEntity.getStock())
                .setLowStock(productEntity.getLowStock())
                .setUnit(productEntity.getUnit())
                .setWeight(productEntity.getWeight())
                .setPreviewStatus(productEntity.getPreviewStatus())
                .setServiceIds(productEntity.getServiceIds())
                .setKeywords(productEntity.getKeywords())
                .setNote(productEntity.getNote())
                .setDetailTitle(productEntity.getDetailTitle())
                .setDetailDesc(productEntity.getDetailDesc())
                .setDetailHtml(productEntity.getDetailHtml())
                .setDetailMobileHtml(productEntity.getDetailMobileHtml())
                .setPromotionStartTime(productEntity.getPromotionStartTime())
                .setPromotionEndTime(productEntity.getPromotionEndTime())
                .setPromotionPerLimit(productEntity.getPromotionPerLimit())
                .setPromotionType(productEntity.getPromotionType())
                .setBrandName(productEntity.getBrandName())
                .setProductCategoryName(productEntity.getProductCategoryName())
                .setCommission(productEntity.getCommission());

        if (StringUtils.isNotBlank(productEntity.getAlbumPics())) {
            List<String> albumPicUrls = StrSpliter.splitTrim(productEntity.getAlbumPics(), ',', true);
            product.setAlbumPicUrls(albumPicUrls);
        }

        return product;
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param productEntities 实体
     * @return products
     */
    @Override
    public List<Product> entityToDomain(List<ProductEntity> productEntities) {
        List<Product> result = new ArrayList<>();
        if (productEntities == null) {
            return result;
        }

        for (ProductEntity productEntity : productEntities) {
            Product product = entityToDomain(productEntity);
            result.add(product);
        }
        return result;
    }

}
